var data = "", flag = 0, total=500;
var timer = new Array();
var access = new Array();
var access_0 = new Array();
var access_1 = new Array();
var access_2 = new Array();
var access_3 = new Array();
var access_7 = new Array();
var access_8 = new Array();
var access_9 = new Array();
var access_20 = new Array();


$(function(){
	$("#body").keydown(function(event){ 
		var key = event.which;
		if( key == 32 ){//第一图 空格
			layout(0);
		}else if( key == 48 || key == 96 ){//0
			layout(1);
			$.cookie('type', '0');
			$.cookie('num', '20');
		}else if( key == 55 || key == 103 ){//7
			//layout(1);
			//$.cookie('type', '0');
			//$.cookie('num', '3');
		}else if( key == 56 || key == 104 ){//8
			//layout(1);
			//$.cookie('type', '0');
			//$.cookie('num', '6');
		}else if( key == 57 || key == 105 ){//9
			//layout(1);
			//$.cookie('type', '0');
			//$.cookie('num', '20');
		}else if( key == 49 || key == 97){//1
			layout(4);
			$.cookie('type', '1'); 
			$.cookie('num', '1'); 
		}else if( key == 50 || key == 98){//2
			layout(3);
			$.cookie('type', '2'); 
			$.cookie('num', '2'); 
		}else if( key == 51 || key == 99){//3
			layout(2);
			$.cookie('type', '3'); 
			$.cookie('num', '3'); 
		}else if( key == 16){//shift
			zioff();
			layout(5);
			loadText();
			$.cookie("ok", "stop");
		}else if( key == 13 ){//enter
			var type = $.cookie('type');
			var num = $.cookie('num');
			//alert(num);return;
			switch(type){
				case '0':
					zioff();
					switch(num){
						case '20':
							$("#zi_20").show();
							$.cookie("int",20);
							fninit('cjg');
							break;
						case '10':
							$("#zi_0").show();
							$.cookie("int",10);
							fninit('cjg');
							break;
						case '9':
							$("#zi_9").show();
							$.cookie("int",9);
							fninit('cjf');
							break;
						case '6':
							$("#zi_8").show();
							$.cookie("int",6);
							fninit('cje');
							break;
						case '3':
							$("#zi_7").show();
							$.cookie("int",3);
							fninit('cjd');
							break;
					}
					break;
				case '1':
					zioff();
					$("#zi_1").show();
					$.cookie("int",1);
					fninit('cja');
					break;
				case '2':
					zioff();
					$("#zi_2").show();
					$.cookie("int",2);
					fninit('cjb');
					break;
				case '3':
					zioff();
					$("#zi_3").show();
					$.cookie("int",3);
					fninit('cjc');
					break;
			}
			
		}else if( key == 27 ){//esc
			layout(0);
			addAccess();
		}
	}); 
	// 自动加载
	layout(0);	//初始化页面
	count();	//获取总数
	user();		//获取用户
});
function layout(n){
	switch(n){
		case 1:
			dell();
			$("#layout").addClass('d2');
			break;
		case 2:
			dell();
			$("#layout").addClass('d3');
			break;
		case 3:
			dell();
			$("#layout").addClass('d4');
			break;
		case 4:
			dell();
			$("#layout").addClass('d5');
			break;
		case 5:
			dell();
			$("#movie").hide();
			$("#layout").hide();
			$("#cj").show();
			break;
		default:
			dell();
			$("#layout").addClass('d1');
	}
}
function movie(){
//	$("#movie").fadeIn("slow");
//	$("#layout").fadeOut("slow");
	$("#movie").show();
	$("#layout").hide();
	$("#cj").hide();
}
function dell(){
	$("#movie").css('display','none');
	$("#cj").css('display','none');
	$("#layout").removeClass('d1');
	$("#layout").removeClass('d2');
	$("#layout").removeClass('d3');
	$("#layout").removeClass('d4');
	$("#layout").removeClass('d5');
	$("#layout").show();
}
function zioff(){
	$("#zi_1").hide();
	$("#zi_2").hide();
	$("#zi_3").hide();

	$("#zi_7").hide();
	$("#zi_8").hide();
	$("#zi_9").hide();

	$("#zi_20").hide();

	$("#zi_0").hide();
}
// 底部默认数值
function loadText(){
	var snum = $.cookie("snum");
	var znum = $.cookie("count");
	var ynum = $.cookie("ynum");
	var nnum = $.cookie("nnum");
	if( typeof(snum) == "undefined" ) {snum = 0;$.cookie("snum",0);}
	if( typeof(znum) == "undefined" ) {znum = 0;$.cookie("znum",0);}
	if( typeof(ynum) == "undefined" ) {ynum = 0;$.cookie("ynum",0);}
	if( typeof(nnum) == "undefined" ) {nnum = 0;$.cookie("nnum",znum);}

	$("#num1").html(snum);
	$("#num2").html(znum);
	$("#num3").html(ynum);
	$("#num4").html(nnum);
}
function upTest(){
	$.cookie('snum', parseInt($.cookie('int')));
	$.cookie('ynum', parseInt($.cookie('ynum')) + parseInt($.cookie('int')));
	$.cookie('nnum', parseInt($.cookie('count')) - parseInt($.cookie('ynum')));
	loadText();
	$.cookie('int',0);
}
function count(){
	var count = $.cookie('count');
	if( typeof(count) != "undefined" && count != ""){
		return count;
	}
	var ajax = $.ajax({type:'GET',url:'ajax.php',data:'type=count',async:false,dataType:'json'});
	var json = $.parseJSON(ajax.responseText);
	$.cookie("count",json.count);
	return(json.count);
}

function user(){
	var user = $.cookie('user');
	if( typeof(user) != "undefined" && user != ""){
		return user;
	}
	var ajax = $.ajax({type:'GET',url:'ajax.php',data:'type=user&num='+total,async:false,dataType:'text'});
	var json = $.parseJSON(ajax.responseText);
	//$.cookie("count",json.count);
	//return(json.count);
	var str = json.user;
	var result = str.split("|");
	data = result;
	return result;
}
function fnplay(o,i){
	clearInterval(timer[i]);
	timer[i]=setInterval(function(){
		//Math.random()拿到的是0-1之前的数字，去乘数组的长度 再取整数可以拿到想要的数组下标
		var random=Math.floor(Math.random()*data.length); //floor去取整
		//把拿到的数组的值写进去
		//o.innerHTML=data[random];
		o.html(data[random]);
	},50);
}
function fnstop(o,i){
	var str = o.html();
	var flag = false;
	var check = checkArray(str,access);
	var ctype = $.cookie('type');
	var cnum = $.cookie('num');
	if( !check ){
		access.push(str);
		if( ctype == '0' && cnum == '20'){
			access_0.push(str);
		}else if( ctype == '0' && cnum == '9'){
			access_9.push(str);
		}else if( ctype == '0' && cnum == '6'){
			access_8.push(str);
		}else if( ctype == '0' && cnum == '3'){
			access_7.push(str);
		}else if( ctype == '1' && cnum == '1'){
			access_1.push(str);
		}else if( ctype == '2' && cnum == '2'){
			access_2.push(str);
		}else if( ctype == '3' && cnum == '3'){
			access_3.push(str);
		}else if( ctype == '0' && cnum == '20'){
			access_20.push(str);
		}
		
		clearInterval(timer[i]);
		flag = true;
	}
	return flag;
}
function fninit(id){
	var ok = $.cookie("ok");
	var n = $.cookie("num");
	if( typeof(ok) != "undefined" && ok == "play"){
		$.cookie("ok", "stop");
		var i = 0;
		for(var i=0; i<n; i++){
			var o = $('.'+id+'_'+i);
			checkStop(o,i);
		}
		upTest();
	}else{
		$.cookie("ok", "play");
		for(var i=0; i<n; i++){
			var o = $('.'+id+'_'+i);
			fnplay(o,i);
		}
	}	
}

function array_move(arr,data){
	for (k=0;k<data.length;k++)
	{
		for (j = 0; j < arr.length; j++)
		{
			if (arr[j]==data[k])
			arr.splice(j, 1);

		}
	}
	return arr;
}

function checkArray(str, data){
	var flag = false;
	if(data.length > 0 ){
		for(i=0;i<data.length;i++){
			if( data[i] == str ){
				flag = true
				break;
			}
		}
	}
	return flag;
}
function checkStop(o,i){
	while(true){
		var random=Math.floor(Math.random()*data.length);
		o.html(data[random]);
		if( fnstop(o,i) ){
			break;
		}
	}
}

function addAccess(){
//	var list = assess;
    $.ajax({
			type: 'POST',
			url : "ajax.php",
			async : false,
			dataType : 'json',
			data:{type:"add",access_0:access_0,access_1:access_1,access_2:access_2,access_3:access_3,access_7:access_7,access_8:access_8,access_9:access_9,access_20:access_20},
			success : function(json){
				if( json.msg == 'ok'){
					//alert(json.data);
				}
			}
			
	});
}

//http://wwi.mark.ok/2016/